package public

import (
	"fmt"
	"go-interview-forum/db/redis"
	"go-interview-forum/global"
	"go-interview-forum/pkg"
	"log"
	"math/rand"
	"strconv"
)

func SendSms(mobile string, from string) error {
	code := strconv.Itoa(rand.Intn(8999) + 1000)

	err := pkg.SendSms(mobile, code)
	if err != nil {
		return err
	}

	var key string
	switch from {
	case "login":
		key = global.SMS_LOGIN
	default:
		log.Printf("未知来源:%v\n", from)
		return fmt.Errorf("请使用正确的来源")
	}

	return redis.SetKey(key+mobile, code)
}
